package com.higgins.lawapp.client.settings.panels;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.higgins.lawapp.client.settings.components.ButtonsPanel;
import com.higgins.lawapp.client.settings.components.DataSourceComponent;
import com.higgins.lawapp.client.settings.components.DataSourceElementFields;
import com.higgins.lawapp.client.settings.components.DataSourceFieldFields;
import com.higgins.lawapp.client.settings.components.DataSourceLabelFields;
import com.higgins.lawapp.client.settings.components.NameField;
import com.higgins.lawapp.client.settings.components.URLField;
import com.higgins.lawapp.client.settings.dataModel.DataSourceDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceLevelDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceObjectDTO;
import com.higgins.lawapp.client.settings.model.DataSourceModel;
import com.higgins.lawapp.client.settings.presenter.DataSourcePresenter;


public class SettingsInternalTabsPanel extends DockLayoutPanel	{
	private ButtonsPanel buttonsPanel;
	private SettingsInternalTabs settingsInternalTabs;
	private DataSourceModel dataSourceModel;
	private List<DataSourceComponent> dataSourceFieldWidgets = new ArrayList<DataSourceComponent>();
	private int tabNumber;
	private NameField dataSourceName;
	private URLField dataSourceURL;
	private CheckBox dataSourceExecution;
	public Label errorLabel = new Label();

    
	public SettingsInternalTabsPanel(SettingsInternalTabs settingsInternalTabs, String url, String urlExampleField, DataSourceModel dataSourceModel, DataSourceLevelDTO dataSourceLevelDTO, int tabNumber) {
		super(Unit.EM);
		this.settingsInternalTabs = settingsInternalTabs;
		this.dataSourceModel = dataSourceModel;
		this.tabNumber = tabNumber;
		setWidth("800px");
		setHeight("1200px");
		addSouth(errorLabel, 5);
		if (dataSourceLevelDTO!=null)	{
			addDataSourceField("DataSource Name", dataSourceLevelDTO.getDataSourceName());
			addURLField("DataSource URL", dataSourceLevelDTO.getDataSourceURL(), dataSourceLevelDTO.getDataSourceExampleField(), dataSourceLevelDTO.getDataSourceExecution(), dataSourceLevelDTO.getDataSourceConcat());
		}
		else	{
			addDataSourceField("DataSource Name", "");
			addURLField("DataSource URL", url, urlExampleField, false, true);
		}
		addButtons();
		if (dataSourceLevelDTO!=null)	{
			List<DataSourceObjectDTO> dataSourceLeveDTOs = dataSourceLevelDTO.getDataSourceObjectDTOs();
			Iterator<DataSourceObjectDTO> iterator = dataSourceLeveDTOs.iterator();
			while (iterator.hasNext())	{
				DataSourceObjectDTO dataSourceObjectDTO = iterator.next();
				
				
				if (dataSourceObjectDTO.getDataSourceType().equals("Label"))	{
					String label = dataSourceObjectDTO.getLabel();
					Integer number = dataSourceObjectDTO.getNumber();
					buttonsPanel.addLabelPanel(label, number);
				}
				if (dataSourceObjectDTO.getDataSourceType().equals("Element"))	{
					buttonsPanel.addElementPanel(dataSourceObjectDTO.getStart(), dataSourceObjectDTO.getEnd());
				}
				if (dataSourceObjectDTO.getDataSourceType().equals("Field"))	{
					buttonsPanel.addFieldPanel(dataSourceObjectDTO.getFieldName(), dataSourceObjectDTO.getStart(), dataSourceObjectDTO.getEnd(), dataSourceObjectDTO.getURL(), dataSourceObjectDTO.getConcat());
				}		
			}			
		}
		setHeight("600px");
		setWidth("800px");

	}
	public void addDataSourceField(String fieldName, String fieldText)	{
		dataSourceName = new NameField(fieldName, fieldText, dataSourceModel, tabNumber);
	    addNorth(dataSourceName, 2);
	}
	public void addURLField(String fieldName, String fieldText, String urlExampleField, Boolean execution, Boolean concat)	{
		dataSourceURL = new URLField(fieldName, fieldText, urlExampleField, dataSourceModel, tabNumber, execution, concat);
	    addNorth(dataSourceURL, 2);
	}
	public void addButtons()	{
		buttonsPanel = new ButtonsPanel(dataSourceModel, settingsInternalTabs, this, dataSourceFieldWidgets, tabNumber);
	    final LayoutPanel newFieldLayoutPanel = buttonsPanel;
	    addNorth(newFieldLayoutPanel, 2);
	}
	public List<DataSourceComponent> getDataSourceFieldWidgets()	{
		return dataSourceFieldWidgets;
	}
	public void updateDataSourceLevelDTO(DataSourceLevelDTO dataSourceLevelDTO)	{
		// Name & URL
		dataSourceLevelDTO.setDataSourceName(getDataSourceName());
		dataSourceLevelDTO.setDataSourceURL(getDataSourceURL());
		dataSourceLevelDTO.setDataSourceExecution(getDataSourceExecution());
		dataSourceLevelDTO.setDataSourceExampleField(getDataSourceExampeField());
		dataSourceLevelDTO.setDataSourceConcat(getDataSourceConcat());
	}
	public String getDataSourceName()	{
		return dataSourceName.getDataSourceName();
	}
	public String getDataSourceURL()	{
		return dataSourceURL.getDataSourceURL();
	}
	public Boolean getDataSourceExecution()	{
		return dataSourceURL.getExecution();
	}
	public String getDataSourceExampeField()	{
		return dataSourceURL.getDataSourceURLExampleField();
	}
	public Boolean getDataSourceConcat()	{
		return dataSourceURL.getConcat();
	}
}
